home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / pack / xfh132.lzh / XFH / src / file.c < prev    next >
C/C++ Source or Header  |  1993-01-19  |  8KB  |  253 lines

  1. /* file.c - file-handle stuff for the XFH file system handler.
  2.    Copyright (C) 1991, 1992, 1993 Kristian Nielsen.
  3.  
  4.    This file is part of XFH, the compressing file system handler.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            */
  19.  
  20. /* $Log:    file.c,v $
  21.  * Revision 1.2  93/01/14  15:29:11  Kristian
  22.  * Added RCS keywords.
  23.  * 
  24.  */
  25.  
  26.  
  27. #include "CFS.h"
  28.  
  29. #include <string.h>
  30.  
  31. #include <dossupport.h>
  32.  
  33.  
  34. /*
  35.  * RawCFSOpen() is the low-lewel Open() function of the CFS file
  36.  * system. It takes a simple filename (that is, no path specification),
  37.  * a lock to the parent directory, and a 'mode' value as arguments. A
  38.  * special case occurs for a null name, in which case it opens the file
  39.  * associated with the passed lock.
  40.  */
  41.  
  42. struct CFSFH *RawCFSOpen(glb glob, struct CFSLock *lock,
  43.                         char * name, LONG mode ){
  44.    struct FileHandle *xfh;
  45.    void *fh = NULL;
  46.  
  47.    /* Here is the place to check whether the lock refers to a
  48.     * directory in the underlying file system, or if it is in fact
  49.     * a directory in an archieve. This code is for the former case: */
  50.  
  51.    if( lock->objtype == XOBJECT ){
  52.       LONG type;
  53.       BOOL isnew = FALSE;   /* Whether MODE_READWRITE file is new. */
  54.  
  55.       /* ToDo: Need better way to determine the correct action. */
  56.       if( mode != MODE_OLDFILE && mode != MODE_NEWFILE && mode != MODE_READWRITE ){
  57.          debug(("Error: RawCFSOpen(): Unsupported Open() mode %ld\n",mode));
  58.          glob->ioerr = ERROR_ACTION_NOT_KNOWN;
  59.          return NULL;
  60.       }
  61.  
  62.       if(mode==MODE_READWRITE){
  63.          /* Check that append mode is allowed. */
  64.          if(!glob->allowappend){
  65.             debug(("Append mode not set... failing.\n"));
  66.             glob->ioerr = ERROR_ACTION_NOT_KNOWN;
  67.             return NULL;
  68.          }
  69.          /* Try to determine whether file already exists. NOTE: this is
  70.           * NOT fool-proof, since there is a (slight) change that the
  71.           * file may suddenly appear just before the xOpen() call.
  72.           */
  73.          isnew = !xExists1(glob, lock->xlock, name);
  74.       }
  75.       if( !(xfh=xOpen(glob, lock->xlock, name, mode)) ){
  76.          debug(("RawCFSOpen(): Object %s could not be opened: %d.\n",name,glob->ioerr));
  77.          return NULL;
  78.       }
  79.       if( mode == MODE_NEWFILE ){
  80.          /* NOTE: the name cannot be "" (since then it's already in use). */
  81.          if(!strcmp(name,"")){
  82.             debug(("Strange...empty name in open new file - failing.\n"));
  83.             xClose(glob, xfh);
  84.             xDeleteFile(glob, lock->xlock, name);
  85.             glob->ioerr = ERROR_OBJECT_IN_USE;
  86.             return NULL;
  87.          }
  88.          if(!(fh = XObjCreateFH(glob, xfh, mode, name, lock))){
  89.             SAVEIOERR;
  90.             xClose(glob, xfh);
  91.             xDeleteFile(glob, lock->xlock, name);
  92.             RESTIOERR;
  93.             return NULL;
  94.          }
  95.       }else{   /* MODE_OLDFILE or MODE_READWRITE */
  96.          type = xFileType( glob, xfh );
  97.          /* 'type' will be XObj for new file created with MODE_READWRITE. */
  98.          if( type == XPKOBJECT ){
  99.             if( mode==MODE_OLDFILE ){
  100.                struct CFSFH *newfh=(struct CFSFH *)XpkOpenOldFile( glob, xfh );
  101.                if(!newfh){
  102.                   SAVEIOERR;
  103.                   xClose(glob,xfh);
  104.                   RESTIOERR;
  105.                   return NULL;
  106.                }
  107.            /* Ready for Write() if needed. */
  108.            if(!XObjStuffFH(glob, newfh, name, lock)){
  109.          SAVEIOERR;
  110.          Xpk_Close(glob, (struct XpkFH *)newfh);
  111.          RESTIOERR;
  112.          return NULL;
  113.            }
  114.                fh = newfh;
  115.             }else{     /* MODE_READWRITE */
  116.                BOOL res;
  117.                
  118.                xClose(glob,xfh);
  119.                debug(("Attempting to uncompress file %s.\n",name));
  120.                res = TransformFile(glob, lock->xlock, name, UnPackFile2File, NULL);
  121.                debug(("TransformFile() returned: %ld.\n",res));
  122.                if(!res) return NULL;
  123.                if( !(xfh=xOpen(glob, lock->xlock, name, mode)) ){
  124.                   debug(("Error: File %s could not be re-opened: %d.\n",name,glob->ioerr));
  125.                   return NULL;
  126.                }
  127.                fh = XObjCreateFH(glob, xfh, mode, name, lock);
  128.                if(!fh){
  129.                   SAVEIOERR;
  130.                   xClose(glob, xfh);
  131.                   RESTIOERR;
  132.                   return NULL;
  133.                }
  134.             }
  135.          }else{
  136.             /* A plain file. If the mode is MODE_READWRITE and the file
  137.              * was newly created, the user can request the file to be
  138.              * autocompressed. Otherwise, the file will not be compressed.
  139.              */
  140.             if(mode==MODE_READWRITE && isnew && glob->compressreadwrite){
  141.                fh = XObjCreateFH(glob, xfh, mode, name, lock);
  142.             }else{
  143.                fh = XObjCreateFH(glob, xfh, mode, NULL, NULL);
  144.             }
  145.             if(!fh){
  146.                SAVEIOERR;
  147.                xClose(glob, xfh);
  148.                RESTIOERR;
  149.                return NULL;
  150.             }
  151.          }
  152.       }
  153.    }else if( lock->objtype == XPKOBJECT ){  /* Allow Open(xpklock,""). */
  154.       if( mode != MODE_OLDFILE ){
  155.          debug(("Bad open from XpkLock (mode=%ld).\n",mode));
  156.          if(!strcmp(name,"")){
  157.             glob->ioerr = ERROR_OBJECT_IN_USE;
  158.          }else{
  159.             glob->ioerr = ERROR_OBJECT_WRONG_TYPE;
  160.          }
  161.          return NULL;
  162.       }else if(strcmp(name,"")){
  163.          debug(("Bad open (non-null filename) from Xpklock.\n"));
  164.          glob->ioerr = ERROR_OBJECT_WRONG_TYPE;
  165.          return NULL;
  166.       }else{
  167.          struct CFSFH *newfh = (struct CFSFH *)
  168.        XpkOpenOldFileFromCopyOfLock( glob, (struct XpkLock *)lock );
  169.          if(!newfh){
  170.             return NULL;
  171.          }
  172.      /* Ready for Write() if needed. */
  173.      if(!XObjStuffFH(glob, newfh, name, lock)){
  174.        SAVEIOERR;
  175.        Xpk_Close(glob, (struct XpkFH *)newfh);
  176.        RESTIOERR;
  177.        return NULL;
  178.      }
  179.          fh = newfh;
  180.       }
  181.    }else{
  182.       debug(("** PANIC **: bad object type in lock: %ld\n",lock->objtype));
  183.       glob->ioerr = ERROR_OBJECT_WRONG_TYPE;
  184.       return NULL;
  185.    }
  186.    return fh;
  187. }
  188.  
  189.  
  190. struct CFSFH *CFSOpen(glb glob,struct CFSLock * parentlock,
  191.                           char * name,LONG mode){
  192.    struct CFSLock *parent;
  193.    struct CFSFH *fh;
  194.    LONG saveioerr;
  195.    
  196.    /* Lock the parent directory, and set 'name' to point to the actual
  197.     * name. For a path like 'name:', lock the file/dir and point name to
  198.     * "".
  199.     */
  200.    if(!(parent = CFSLockParent(glob, parentlock, &name ))){
  201.       debug(("Error: CFSOpen(): Could not CFSLockParent(): %ld.\n",glob->ioerr));
  202.       return NULL;
  203.    }
  204.    
  205.    /* Now Lock the file or dir. */
  206.    fh = RawCFSOpen( glob, parent, name, mode );
  207.    saveioerr = glob->ioerr;
  208.    CFSUnLock( glob, parent );
  209.    glob->ioerr = saveioerr;
  210.    
  211.    return fh;
  212. }
  213.  
  214.  
  215. BOOL CFSClose( glb glob, struct CFSFH *fh ){
  216.  
  217.    if( !fh ) return DOSTRUE;
  218.    return (BOOL) (*fh->f->Close)( glob, fh );
  219. }
  220.  
  221.  
  222. LONG CFSRead( glb glob, struct CFSFH *fh, void *buf, LONG len ){
  223.    
  224.    if( !fh )
  225.       return 0L;
  226.    else
  227.       return (*fh->f->Read)( glob, fh, buf, len );
  228. }
  229.  
  230.  
  231. LONG CFSWrite( glb glob, struct CFSFH *fh, void *buf, LONG len ){
  232.    
  233.    if( !fh )
  234.       return 0L;
  235.    else
  236.       return (*fh->f->Write)( glob, fh, buf, len );
  237. }
  238.  
  239.  
  240. LONG CFSSeek( glb glob, struct CFSFH *fh, LONG pos, LONG offset ){
  241.    
  242.    if( !fh )
  243.       return -1L;
  244.    else if( !fh->f ){
  245.       debug(("** PANIC **: CFSSeek(): NULL action func.\n"));
  246.       glob->ioerr = ERROR_OBJECT_WRONG_TYPE;
  247.       return -1L;
  248.    }else
  249.       return (*fh->f->Seek)( glob, fh, pos, offset );
  250. }
  251.  
  252. /* End of file.c */
  253.